package com.shawn.baseappbygroovydsl.component

import android.graphics.Canvas
import android.graphics.Color
import android.graphics.LinearGradient
import android.graphics.Paint
import android.graphics.Path
import android.graphics.Rect
import android.graphics.RectF
import android.graphics.Shader
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ItemDecoration
import com.shawn.baseappbygroovydsl.ui.extension.ExtensionCommon.dp

/**
 * @author Secret
 * @since 2023/6/2
 */
class ItemDecorationTask : ItemDecoration() {

    private val mBounds = Rect()
    private val corner = 15f.dp
    private val paint by lazy {
        val p = Paint()
        p.color = Color.WHITE
        p.isDither = true
        p
    }

    private val startOffset = 10f.dp.toInt()
    private val endOffset = 10f.dp.toInt()
    private val topOffset = 16f.dp.toInt()

    private val size1 = 50f.dp.toInt()
    private val dp1 = 1f.dp.toInt()

    override fun getItemOffsets(
        outRect: Rect,
        view: View,
        parent: RecyclerView,
        state: RecyclerView.State
    ) {
        super.getItemOffsets(outRect, view, parent, state)
        val adapter = parent.adapter
        adapter?.let {
            val position = parent.getChildAdapterPosition(view)
            if (position == it.itemCount - 1) {
                outRect.bottom = size1
            }
            if(position == 1){
                outRect.top = topOffset
            }
        }
    }

    override fun onDraw(canvas: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        super.onDraw(canvas, parent, state)
        val itemCnt = parent.adapter?.itemCount ?: 0
        if (itemCnt > 0) {
            for (i in 0 until parent.childCount) {
                val child = parent.getChildAt(i)
                val adapterPos = parent.getChildAdapterPosition(child)
                if (adapterPos >= 1) {
                    val left: Int
                    val right: Int
                    //noinspection AndroidLintNewApi - NewApi lint fails to handle overrides.
                    if (parent.clipToPadding) {
                        left = parent.getPaddingLeft() + startOffset
                        right = parent.width - parent.getPaddingRight() - endOffset
                        canvas.clipRect(
                            left, parent.paddingTop, right,
                            parent.height - parent.paddingBottom
                        )
                    } else {
                        left = startOffset
                        right = parent.width - endOffset
                    }

                    parent.getDecoratedBoundsWithMargins(child, mBounds)
                    val bottom = mBounds.bottom + child.translationY
                    val top = bottom - mBounds.height()
                    when (adapterPos) {
                        1 -> {
                            canvas.drawRoundRect(
                                left.toFloat(), top + topOffset, right.toFloat(), bottom, corner, corner, paint
                            )
                            canvas.drawRect(left.toFloat(),top + child.height / 2f + topOffset,right.toFloat(),bottom,paint)
                            val gradientPaint = Paint().apply {
                                shader = LinearGradient(
                                    left.toFloat() + dp1, top + topOffset + dp1,
                                    left.toFloat() + dp1, bottom,
                                    intArrayOf(Color.parseColor("#FEE9E6"), Color.parseColor("#99FEE9E6")),
                                    null,
                                    Shader.TileMode.CLAMP
                                )
                            }
                            val path = Path().apply {
                                moveTo(left.toFloat() + corner + dp1, top + topOffset + dp1)
                                lineTo(right.toFloat() - corner - dp1, top + topOffset + dp1)
                                arcTo(
                                    RectF(right.toFloat() - 2 * corner - dp1, top + topOffset + dp1,
                                        right.toFloat() - dp1, top + topOffset + 2 * corner + dp1),
                                    -90f, 90f
                                )
                                lineTo(right.toFloat() - dp1, bottom)
                                lineTo(left.toFloat() + dp1, bottom)
                                lineTo(left.toFloat() + dp1, top + topOffset + corner + dp1)
                                arcTo(
                                    RectF(left.toFloat() + dp1, top + topOffset + dp1,
                                        left.toFloat() + 2 * corner + dp1, top + topOffset + 2 * corner + dp1),
                                    180f, 90f
                                )
                                close()
                            }
                            canvas.save()
                            canvas.clipPath(path)
                            canvas.drawRect(
                                left.toFloat(), top + topOffset,
                                right.toFloat(), bottom,
                                gradientPaint
                            )
                            canvas.restore()
                        }

                        2 -> {
                            canvas.drawRect(left.toFloat(), top, right.toFloat(), bottom, paint)
                            val gradientPaint = Paint().apply {
                                shader = LinearGradient(
                                    left.toFloat() + dp1, top,
                                    left.toFloat() + dp1, bottom,
                                    intArrayOf(Color.parseColor("#99FEE9E6"), Color.WHITE),
                                    null,
                                    Shader.TileMode.CLAMP
                                )
                            }
                            canvas.drawRect(
                                left.toFloat() + dp1,
                                top,
                                right.toFloat() - dp1,
                                bottom,
                                gradientPaint
                            )
                        }

                        itemCnt - 1 -> {
                            canvas.drawRoundRect(
                                left.toFloat(), top, right.toFloat(),
                                bottom - size1, corner, corner, paint
                            )
                            canvas.drawRect(left.toFloat(),top,right.toFloat(),bottom - child.height / 2f - size1,paint)
                        }

                        else -> canvas.drawRect(left.toFloat(),top,right.toFloat(),bottom,paint)
                    }
                }
            }
        }
    }

}